package com.mentaleye.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mentaleye.bean.WorkTimeBean;
import com.mentaleye.dal.model.StudentAttendanceRecord;
import com.mentaleye.dal.model.StudentDiscipline;
import com.mentaleye.dal.model.StudentDisciplineRecord;
import com.mentaleye.dal.model.StudentPunchRecord;
import com.mentaleye.enums.DisciplineEnums;

public class AttendanceDataUtil {
	public static List<Integer> getStudentIdList(List<StudentDisciplineRecord> disciplineRecordList) {
		List<Integer> studentIdList = new ArrayList<Integer>();
		for (StudentDisciplineRecord disciplineRecord : disciplineRecordList) {
			studentIdList.add(disciplineRecord.getStudentId());
		}
		return studentIdList;
	}

	public static List<Date> getWriteTimeList(List<StudentDisciplineRecord> disciplineRecordList) {
		List<Date> writeTimeList = new ArrayList<Date>();
		for (StudentDisciplineRecord disciplineRecord : disciplineRecordList) {
			writeTimeList.add(disciplineRecord.getWriteTime());
		}
		return writeTimeList;
	}

	public static Map<String, Object> getAttendanceListByList(float requireNum, List<StudentPunchRecord> punchList,
			List<StudentDiscipline> disciplineList, List<StudentDisciplineRecord> disciplineRecordList,
			WorkTimeBean workTimeBean) throws IOException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int n = 0;
		float earlyNum = 0;
		float earlyDeductNum = 0;
		float lateNum = 0;
		float lateDeductNum = 0;
		float sickNum = 0;
		float sickDeductNum = 0;
		float matterNum = 0;
		float matterDeductNum = 0;
		float truantNum = 0;
		float truantDeductNum = 0;
		float deductNum = 0;
		float realNum = 0;
		String beginTime = workTimeBean.getOfficeHour();
		String endTime = workTimeBean.getClosingTime();
		int studentId = 0;
		String writeTimeStr = null;
		String beginDateTimeStr = null;
		String endDateTimeStr = null;
		Date beginDateTimeDt = new Date();
		Date endDateTimeDt = new Date();
		String firstTime = null;
		String lastTime = null;
		Date firstTimeDt = new Date();
		Date lastTimeDt = new Date();
		StudentAttendanceRecord attendanceRecord = null;
		StudentDisciplineRecord disciplineRecordIe = null;
		List<StudentAttendanceRecord> recordList = new ArrayList<StudentAttendanceRecord>();
		List<StudentDisciplineRecord> dRRecordList = new ArrayList<StudentDisciplineRecord>();

		if (disciplineList.isEmpty()) {
			earlyDeductNum = 0;
			lateDeductNum = 0;
			sickDeductNum = 0;
			matterDeductNum = 0;
			truantDeductNum = 0;
		} else {
			for (StudentDiscipline discipline : disciplineList) {
				if (discipline.getDisciplineId() == DisciplineEnums.LEAVE_EARLY.getCode()) {
					earlyDeductNum = discipline.getDeductNum();
				}
				if (discipline.getDisciplineId() == DisciplineEnums.BE_LATE.getCode()) {
					lateDeductNum = discipline.getDeductNum();
				}
				if (discipline.getDisciplineId() == DisciplineEnums.SICK_LEAVE.getCode()) {
					sickDeductNum = discipline.getDeductNum();
				}
				if (discipline.getDisciplineId() == DisciplineEnums.MATTER_LEAVE.getCode()) {
					matterDeductNum = discipline.getDeductNum();
				}
				if (discipline.getDisciplineId() == DisciplineEnums.TRUANT.getCode()) {
					truantDeductNum = discipline.getDeductNum();
				}
			}
		}

		if (!punchList.isEmpty()) {
			for (StudentPunchRecord punchRecord : punchList) {
				if (n > 0) {
					if (punchRecord.getStudentId() != studentId) {
						truantNum = truantNum - sickNum - matterNum;
						realNum = requireNum - sickNum - matterNum - truantNum;
						deductNum = earlyNum * earlyDeductNum + lateNum * lateDeductNum + sickNum * sickDeductNum
								+ matterNum * matterDeductNum + truantNum * truantDeductNum;

						attendanceRecord = new StudentAttendanceRecord();
						attendanceRecord.setStudentId(studentId);
						attendanceRecord.setWriteTime(punchRecord.getWriteTime());
						attendanceRecord.setRequireNum(requireNum);
						attendanceRecord.setRealNum(realNum);
						attendanceRecord.setEarlyNum(earlyNum);
						attendanceRecord.setLateNum(lateNum);
						attendanceRecord.setSickNum(sickNum);
						attendanceRecord.setMatterNum(matterNum);
						attendanceRecord.setTruantNum(truantNum);
						attendanceRecord.setDeductNum(deductNum);
						attendanceRecord.setRemark("");

						recordList.add(attendanceRecord);

						earlyNum = 0;
						lateNum = 0;
						sickNum = 0;
						matterNum = 0;
						truantNum = 0;

						if ((punchRecord.getFirstTime() == null || punchRecord.getFirstTime().equals(""))
								&& (punchRecord.getLastTime() == null || punchRecord.getLastTime().equals(""))) {
							if (disciplineRecordList.isEmpty()) {
								truantNum++;
								disciplineRecordIe = new StudentDisciplineRecord();
								disciplineRecordIe.setStudentId(punchRecord.getStudentId());
								disciplineRecordIe.setDisciplineId(DisciplineEnums.TRUANT.getCode());
								disciplineRecordIe.setWriteTime(punchRecord.getWriteTime());
								disciplineRecordIe.setRemark("");
								dRRecordList.add(disciplineRecordIe);
							} else {
								for (StudentDisciplineRecord disciplineRecord : disciplineRecordList) {
									if (disciplineRecord.getStudentId() == punchRecord.getStudentId()
											&& disciplineRecord.getDisciplineId() == DisciplineEnums.SICK_LEAVE
													.getCode()
											&& punchRecord.getWriteTime().equals(disciplineRecord.getWriteTime())) {
										sickNum++;
										punchRecord.setRemark("病假");
									}
									if (disciplineRecord.getStudentId() == punchRecord.getStudentId()
											&& disciplineRecord.getDisciplineId() == DisciplineEnums.MATTER_LEAVE
													.getCode()
											&& punchRecord.getWriteTime().equals(disciplineRecord.getWriteTime())) {
										matterNum++;
										punchRecord.setRemark("事假");
									}
									if (disciplineRecord.getStudentId() == punchRecord.getStudentId()
											&& punchRecord.getWriteTime().equals(disciplineRecord.getWriteTime())) {
										truantNum++;
										disciplineRecordIe = new StudentDisciplineRecord();
										disciplineRecordIe.setStudentId(punchRecord.getStudentId());
										disciplineRecordIe.setDisciplineId(DisciplineEnums.TRUANT.getCode());
										disciplineRecordIe.setWriteTime(punchRecord.getWriteTime());
										disciplineRecordIe.setRemark("");
										dRRecordList.add(disciplineRecordIe);
									}
								}
							}
						} else if (punchRecord.getFirstTime() != null && !punchRecord.getFirstTime().equals("")) {
							writeTimeStr = DateFormatter.format(punchRecord.getWriteTime(), "yyyy-MM-dd");
							beginDateTimeStr = writeTimeStr + " " + beginTime;
							beginDateTimeDt = DateFormatter.parse(beginDateTimeStr, "yyyy-MM-dd hh:mm");
							firstTime = writeTimeStr + " " + punchRecord.getFirstTime();
							firstTimeDt = DateFormatter.parse(firstTime, "yyyy-MM-dd hh:mm");
							if (firstTimeDt.after(beginDateTimeDt)) {
								lateNum++;
								disciplineRecordIe = new StudentDisciplineRecord();
								disciplineRecordIe.setStudentId(punchRecord.getStudentId());
								disciplineRecordIe.setDisciplineId(DisciplineEnums.BE_LATE.getCode());
								disciplineRecordIe.setWriteTime(punchRecord.getWriteTime());
								disciplineRecordIe.setRemark("");
								dRRecordList.add(disciplineRecordIe);
							}
						} else if (punchRecord.getLastTime() != null && !punchRecord.getLastTime().equals("")) {
							writeTimeStr = DateFormatter.format(punchRecord.getWriteTime(), "yyyy-MM-dd");
							endDateTimeStr = writeTimeStr + " " + endTime;
							endDateTimeDt = DateFormatter.parse(endDateTimeStr, "yyyy-MM-dd hh:mm");
							lastTime = writeTimeStr + " " + punchRecord.getFirstTime();
							lastTimeDt = DateFormatter.parse(lastTime, "yyyy-MM-dd hh:mm");
							if (lastTimeDt.before(endDateTimeDt)) {
								earlyNum++;
								disciplineRecordIe = new StudentDisciplineRecord();
								disciplineRecordIe.setStudentId(punchRecord.getStudentId());
								disciplineRecordIe.setDisciplineId(DisciplineEnums.LEAVE_EARLY.getCode());
								disciplineRecordIe.setWriteTime(punchRecord.getWriteTime());
								disciplineRecordIe.setRemark("");
								dRRecordList.add(disciplineRecordIe);
							}
						}
					} else {
						if ((punchRecord.getFirstTime() == null || punchRecord.getFirstTime().equals(""))
								&& (punchRecord.getLastTime() == null || punchRecord.getLastTime().equals(""))) {
							if (disciplineRecordList.isEmpty()) {
								truantNum++;
								disciplineRecordIe = new StudentDisciplineRecord();
								disciplineRecordIe.setStudentId(punchRecord.getStudentId());
								disciplineRecordIe.setDisciplineId(DisciplineEnums.TRUANT.getCode());
								disciplineRecordIe.setWriteTime(punchRecord.getWriteTime());
								disciplineRecordIe.setRemark("");
								dRRecordList.add(disciplineRecordIe);
							} else {
								for (StudentDisciplineRecord disciplineRecord : disciplineRecordList) {
									if (disciplineRecord.getStudentId() == punchRecord.getStudentId()
											&& disciplineRecord.getDisciplineId() == DisciplineEnums.SICK_LEAVE
													.getCode()
											&& punchRecord.getWriteTime().equals(disciplineRecord.getWriteTime())) {
										sickNum++;
										punchRecord.setRemark("病假");
									}
									if (disciplineRecord.getStudentId() == punchRecord.getStudentId()
											&& disciplineRecord.getDisciplineId() == DisciplineEnums.MATTER_LEAVE
													.getCode()
											&& punchRecord.getWriteTime().equals(disciplineRecord.getWriteTime())) {
										matterNum++;
										punchRecord.setRemark("事假");
									}
									if (disciplineRecord.getStudentId() == punchRecord.getStudentId()
											&& punchRecord.getWriteTime().equals(disciplineRecord.getWriteTime())) {
										truantNum++;
										disciplineRecordIe = new StudentDisciplineRecord();
										disciplineRecordIe.setStudentId(punchRecord.getStudentId());
										disciplineRecordIe.setDisciplineId(DisciplineEnums.TRUANT.getCode());
										disciplineRecordIe.setWriteTime(punchRecord.getWriteTime());
										disciplineRecordIe.setRemark("");
										dRRecordList.add(disciplineRecordIe);
									}
								}
							}
						} else if (punchRecord.getFirstTime() != null && !punchRecord.getFirstTime().equals("")) {
							writeTimeStr = DateFormatter.format(punchRecord.getWriteTime(), "yyyy-MM-dd");
							beginDateTimeStr = writeTimeStr + " " + beginTime;
							beginDateTimeDt = DateFormatter.parse(beginDateTimeStr, "yyyy-MM-dd hh:mm");
							firstTime = writeTimeStr + " " + punchRecord.getFirstTime();
							firstTimeDt = DateFormatter.parse(firstTime, "yyyy-MM-dd hh:mm");
							if (firstTimeDt.after(beginDateTimeDt)) {
								lateNum++;
								disciplineRecordIe = new StudentDisciplineRecord();
								disciplineRecordIe.setStudentId(punchRecord.getStudentId());
								disciplineRecordIe.setDisciplineId(DisciplineEnums.BE_LATE.getCode());
								disciplineRecordIe.setWriteTime(punchRecord.getWriteTime());
								disciplineRecordIe.setRemark("");
								dRRecordList.add(disciplineRecordIe);
							}
						} else if (punchRecord.getLastTime() != null && !punchRecord.getLastTime().equals("")) {
							writeTimeStr = DateFormatter.format(punchRecord.getWriteTime(), "yyyy-MM-dd");
							endDateTimeStr = writeTimeStr + " " + endTime;
							endDateTimeDt = DateFormatter.parse(endDateTimeStr, "yyyy-MM-dd hh:mm");
							lastTime = writeTimeStr + " " + punchRecord.getFirstTime();
							lastTimeDt = DateFormatter.parse(lastTime, "yyyy-MM-dd hh:mm");
							if (lastTimeDt.before(endDateTimeDt)) {
								earlyNum++;
								disciplineRecordIe = new StudentDisciplineRecord();
								disciplineRecordIe.setStudentId(punchRecord.getStudentId());
								disciplineRecordIe.setDisciplineId(DisciplineEnums.LEAVE_EARLY.getCode());
								disciplineRecordIe.setWriteTime(punchRecord.getWriteTime());
								disciplineRecordIe.setRemark("");
								dRRecordList.add(disciplineRecordIe);
							}
						}
					}
				} else {
					if ((punchRecord.getFirstTime() == null || punchRecord.getFirstTime().equals(""))
							&& (punchRecord.getLastTime() == null || punchRecord.getLastTime().equals(""))) {
						if (disciplineRecordList.isEmpty()) {
							truantNum++;
							disciplineRecordIe = new StudentDisciplineRecord();
							disciplineRecordIe.setStudentId(punchRecord.getStudentId());
							disciplineRecordIe.setDisciplineId(DisciplineEnums.TRUANT.getCode());
							disciplineRecordIe.setWriteTime(punchRecord.getWriteTime());
							disciplineRecordIe.setRemark("");
							dRRecordList.add(disciplineRecordIe);
						} else {
							for (StudentDisciplineRecord disciplineRecord : disciplineRecordList) {
								if (disciplineRecord.getStudentId() == punchRecord.getStudentId()
										&& disciplineRecord.getDisciplineId() == DisciplineEnums.SICK_LEAVE.getCode()
										&& punchRecord.getWriteTime().equals(disciplineRecord.getWriteTime())) {
									sickNum++;
									punchRecord.setRemark("病假");
								}
								if (disciplineRecord.getStudentId() == punchRecord.getStudentId()
										&& disciplineRecord.getDisciplineId() == DisciplineEnums.MATTER_LEAVE.getCode()
										&& punchRecord.getWriteTime().equals(disciplineRecord.getWriteTime())) {
									matterNum++;
									punchRecord.setRemark("事假");
								}
								if (disciplineRecord.getStudentId() == punchRecord.getStudentId()
										&& punchRecord.getWriteTime().equals(disciplineRecord.getWriteTime())) {
									truantNum++;
									disciplineRecordIe = new StudentDisciplineRecord();
									disciplineRecordIe.setStudentId(punchRecord.getStudentId());
									disciplineRecordIe.setDisciplineId(DisciplineEnums.TRUANT.getCode());
									disciplineRecordIe.setWriteTime(punchRecord.getWriteTime());
									disciplineRecordIe.setRemark("");
									dRRecordList.add(disciplineRecordIe);
								}
							}
						}
					} else if (punchRecord.getFirstTime() != null && !punchRecord.getFirstTime().equals("")) {
						writeTimeStr = DateFormatter.format(punchRecord.getWriteTime(), "yyyy-MM-dd");
						beginDateTimeStr = writeTimeStr + " " + beginTime;
						beginDateTimeDt = DateFormatter.parse(beginDateTimeStr, "yyyy-MM-dd hh:mm");
						firstTime = writeTimeStr + " " + punchRecord.getFirstTime();
						firstTimeDt = DateFormatter.parse(firstTime, "yyyy-MM-dd hh:mm");
						if (firstTimeDt.after(beginDateTimeDt)) {
							lateNum++;
							disciplineRecordIe = new StudentDisciplineRecord();
							disciplineRecordIe.setStudentId(punchRecord.getStudentId());
							disciplineRecordIe.setDisciplineId(DisciplineEnums.BE_LATE.getCode());
							disciplineRecordIe.setWriteTime(punchRecord.getWriteTime());
							disciplineRecordIe.setRemark("");
							dRRecordList.add(disciplineRecordIe);
						}
					} else if (punchRecord.getLastTime() != null && !punchRecord.getLastTime().equals("")) {
						writeTimeStr = DateFormatter.format(punchRecord.getWriteTime(), "yyyy-MM-dd");
						endDateTimeStr = writeTimeStr + " " + endTime;
						endDateTimeDt = DateFormatter.parse(endDateTimeStr, "yyyy-MM-dd hh:mm");
						lastTime = writeTimeStr + " " + punchRecord.getFirstTime();
						lastTimeDt = DateFormatter.parse(lastTime, "yyyy-MM-dd hh:mm");
						if (lastTimeDt.before(endDateTimeDt)) {
							earlyNum++;
							disciplineRecordIe = new StudentDisciplineRecord();
							disciplineRecordIe.setStudentId(punchRecord.getStudentId());
							disciplineRecordIe.setDisciplineId(DisciplineEnums.LEAVE_EARLY.getCode());
							disciplineRecordIe.setWriteTime(punchRecord.getWriteTime());
							disciplineRecordIe.setRemark("");
							dRRecordList.add(disciplineRecordIe);
						}
					}
				}

				studentId = punchRecord.getStudentId();
				n++;
			}
		}

		resultMap.put("recordList", recordList);
		resultMap.put("dRRecordList", dRRecordList);

		return resultMap;
	}
}
